const Router = require('koa-router');
const ClassRoomRouter = new Router();
const ClassRoomService = require('../service/ClassRoomService');
const R = require('../utils/R');

ClassRoomRouter.prefix('/api/v1/classroom');

ClassRoomRouter.get('/list',async (ctx, next) => {
    let {pageSize, pageNum, level } = ctx.request.query;

    let data = await ClassRoomService.findAll(pageSize, pageNum, level);
    if(data!=null && data.errors){
        ctx.body = R.error(202, data.errors[0].message)
    }else {
        data.pageSize = pageSize;
        data.pageNum = pageNum;
        ctx.body = R.okData(data);
    }
})

// ClassRoomRouter.get('/list/:level', async (ctx, next) => {
//     let level = ctx.params.level;
//     let {pageSize, pageNum } = ctx.request.query;
//     let data = await ClassRoomService.findAll(pageSize, pageNum, level);

//     if(data!=null && data.errors){
//         ctx.body = R.error(202, data.errors[0].message)
//     }else {
//         ctx.body = R.okData(data);
//     }
// })

ClassRoomRouter.post('/save', async (ctx, next) => {
    let roomObj = ctx.request.body;
    let data = await ClassRoomService.saveRoom(roomObj, 1);
    // console.log('data--->', data);
    if(data.name){
        ctx.body = R.errorMsg(data.name)
    }else {
        ctx.body = R.okMsg('添加成功');
    }
})

ClassRoomRouter.post('/delete', async (ctx, next) => {
    let roomObj = ctx.request.body;
    let data = await ClassRoomService.deleteRoom(roomObj.classRoomId);
    // console.log('data--->', data);
    if(data.name || data == 0){
        ctx.body = R.errorMsg(data.name || '删除失败')
    }else {
        ctx.body = R.okMsg('删除成功')
    }
})

ClassRoomRouter.post('/update', async (ctx, next) => {
    let roomObj = ctx.request.body;
    let data = await ClassRoomService.updateRoom(roomObj);
    // console.log('data--->', data[0]);
    if(data.name || data[0] == 0){
        ctx.body = R.errorMsg(data.name || '修改失败')
    }else {
        ctx.body = R.okMsg('修改成功')
    }
})

module.exports = ClassRoomRouter